package com.example.JavaWebApi.model.configmodel;

import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @program: cnu-api-java
 * @description:
 * @author: 杨泽
 * @create: 2023-09-01 14:17
 */

@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;
    private String transcodingDir;
    private Integer expiry;
    private Boolean validateUrl;
    private String callbackurl;
    private String trancodeurl;

    /**
     * Description: 获取默认访问路径
     *
     * @param fileName 文件名
     * @return java.lang.String
     * @author 杨泽
     * @date 2023/9/1 15:49
     */
    public String getDefaultPreviewUrl(String fileName) {
        return (endpoint.endsWith("/") ? endpoint : endpoint + "/") + (defaultBucket.endsWith("/") ? defaultBucket : defaultBucket + "/") + fileName;
    }

    @SneakyThrows
    public String getDownloadUrl(String fileName) {
        return (endpoint.endsWith("/") ? endpoint : endpoint + "/")
                + "/api/v1/buckets/"
                + (defaultBucket.endsWith("/") ? defaultBucket : defaultBucket + "/")
                + "/objects/download?prefix="
                + URLEncoder.encode("fileName", "utf-8");
    }
}
